package com.orangecy.common.util;


import java.lang.reflect.Field;

/**
 * @Author : orangeCy.奕
 * @Product : 猎豹养成产品源码
 * @Time : 2021/9/12
 * @Desc :
 */
public class ReflectionUtils {

    public static Object getFieldValue(String fieldName,Object clazz) {
        try {
            Field field = clazz.getClass().getDeclaredField(fieldName);
            return getFieldValue(field,clazz);
        }catch (Exception ex){
            throw new RuntimeException();
        }
    }

    public static Object getFieldValue(Field field,Object object) {
        boolean accessible = field.isAccessible();
        try {
            if(!accessible) {
                throw new RuntimeException();
            }
            return field.get(object);
        }catch (IllegalAccessException ex) {
            return null;
        }finally {
            field.setAccessible(accessible);
        }
    }
}